HOME/leetcode/

[121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)

Article Outline
TOC
Collection Outline

121. Best Time to Buy and Sell Stock

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        length = len(prices)
        minprices = float("inf")
        maxprofit = 0 
        for i in range(length): 
            if prices[i] < minprices: 
            # 找到最低的点,如果不是最低点,就有可能产生maxprofit的点
                minprices = prices[i]
            elif (prices[i] - minprices) > maxprofit:
                maxprofit = prices[i] -minprices
        return maxprofit